package com.topvision.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.topvision.constants.RoleConstants;
import com.topvision.model.shrio.SysUser;
import com.topvision.model.sys.SysUserDetail;
import com.topvision.model.sys.SysUserVo;
import com.topvision.service.shrio.SysUserService;
import com.topvision.service.sys.SysUserDetailService;
import com.topvision.util.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component("synchronizedUserTask")
public class SynchronizedUserTask {
    private Logger logger = LoggerFactory.getLogger(getClass());
    public final String SYNCHRONIZE_USER_URL = "http://123.233.113.99:89/Handler/AInterface.ashx?Command=synchronizeUsers";
    public final String CHANNEL_KE_DU = "1";

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserDetailService sysUserDetailService;

    public void synchronizedUser() {
        //默认为普通用户
        List<Long> roles = new ArrayList<>();
        roles.add(RoleConstants.ROLE_CONSUMER);
        long startTime = System.currentTimeMillis();
        //调用科渡用户接口
        String result = HttpClientUtil.getGetResult(SYNCHRONIZE_USER_URL, null);
        try {
            JSONObject jsonObject = (JSONObject) JSON.parse(result);
            boolean success = jsonObject.getBoolean("success");
            if (success) {
                String usersJson = jsonObject.getString("users");
                List<SysUserVo> sysUserVos = JSON.parseArray(usersJson, SysUserVo.class);
                SysUser sysUser = new SysUser();
                SysUserDetail sysUserDetail = new SysUserDetail();
                for (SysUserVo sysUserVo : sysUserVos) {
                    //和管理员名字相同则忽略
                    if ("admin".equals(sysUserVo.getUsername())) {
                        continue;
                    }
                    SysUser dbUser = sysUserService.queryByUserName(sysUserVo.getUsername());
                    if (dbUser != null) {
                        //修改
                        dbUser.setPassword(sysUser.getPassword());
                        dbUser.setMobile(sysUserVo.getMobile());
                        dbUser.setEmail(sysUserVo.getEmail());
                        dbUser.setRoleIdList(roles);
                        sysUserService.updateBySynchronizedUser(dbUser);

                        sysUserDetail.setDept(sysUserVo.getDept());
                        sysUserDetail.setJob(sysUserVo.getJob());
                        sysUserDetail.setTruename(sysUserVo.getTrueName());
                        sysUserDetail.setUserId(dbUser.getUserId());
                        sysUserDetailService.updateByUserId(sysUserDetail);

                    } else {
                        //添加
                        sysUser.setUserId(null);
                        sysUser.setUsername(sysUserVo.getUsername());
                        sysUser.setPassword(sysUserVo.getPassword());
                        sysUser.setStatus(1);
                        sysUser.setCreateUserId(1L);
                        sysUser.setCreateTime(new Date());
                        sysUser.setRoleIdList(roles);
                        sysUser = sysUserService.saveBySynchronizedUser(sysUser);

                        sysUserDetail.setChannel(CHANNEL_KE_DU);
                        sysUserDetail.setDept(sysUserVo.getDept());
                        sysUserDetail.setJob(sysUserVo.getJob());
                        sysUserDetail.setTruename(sysUserVo.getTrueName());
                        sysUserDetail.setUserId(sysUser.getUserId());
                        sysUserDetailService.save(sysUserDetail);
                    }


                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("同步用花费了" + String.valueOf(System.currentTimeMillis() - startTime));

    }
}
